package com.xm.xmapp.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.xm.xmapp.R;
import com.xm.xmapp.YHQActivity;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.MyBaseAdapter;
import com.xm.xmapp.base.ViewHolder;
import com.xm.xmapp.response.bean.ResponseUserCouponsList;
import com.xm.xmapp.tools.StringTools;

public class YHQAdapter extends MyBaseAdapter<ResponseUserCouponsList> {

	private int flag;
	private int isSeclect;
	private int type;
	private ResponseUserCouponsList bean;

	public YHQAdapter(ArrayList<ResponseUserCouponsList> list,
			Activity context, int layoutId) {
		super(list, context, layoutId);

	}

	public YHQAdapter(int type, ResponseUserCouponsList bean, int flag,
			ArrayList<ResponseUserCouponsList> list, Activity activity,
			int yhqframentItem) {

		super(list, activity, yhqframentItem);
		this.flag = flag;
		this.type = type;
		this.isSeclect = -1;
		this.bean = bean;

	}

	@Override
	public void setValues(ViewHolder holder,
			final ResponseUserCouponsList t, final int position) {

		View view = holder.getConvertView();

		if (t.coupons_type == 1) {
			view.setBackgroundResource(R.mipmap.coupon_img_certificate_bg_1);
			String str = StringTools.formatFloatNumberMoney(t.coupons_money);
			SpannableString spanString = new SpannableString("￥" + str);

			AbsoluteSizeSpan span = new AbsoluteSizeSpan((int) activity
					.getResources().getDimension(R.dimen.maxsize2));
			spanString.setSpan(span, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

			span = new AbsoluteSizeSpan((int) activity.getResources()
					.getDimension(R.dimen.smallsize3));
			spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

			if (t.coupons_use_money_condition == 0)
				holder.setText(R.id.text1, "现金券");
			else
				holder.setText(R.id.text1, "现金券(满"
						+ (int)t.coupons_use_money_condition + "元可用)");

			holder.setText(R.id.text0, spanString);
			holder.setText(R.id.text2, "在线支付可享受现金优惠");
		} else {
			view.setBackgroundResource(R.mipmap.coupon_img_certificate_bg_2);
			if (t.coupons_use_money_condition == 0)
				holder.setText(R.id.text1, "折扣券");
			else
				holder.setText(R.id.text1, "现金券(满"
						+ (int)t.coupons_use_money_condition + "元可用)");

			SpannableString spanString = new SpannableString(t.coupons_discount
					/ 10 + "折");
			AbsoluteSizeSpan span = new AbsoluteSizeSpan((int) activity
					.getResources().getDimension(R.dimen.maxsize2));
			spanString.setSpan(span, 0, spanString.length() - 1,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			span = new AbsoluteSizeSpan((int) activity.getResources()
					.getDimension(R.dimen.smallsize3));
			spanString.setSpan(span, spanString.length() - 1,
					spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			holder.setText(R.id.text0, spanString);
			holder.setText(R.id.text2, "在线支付可享受折扣优惠");
		}
		if (t.is_restrict_time == 0) {
			holder.setText(R.id.text3, "此券不限制使用时间");
		} else {
			if (t.start_date == null || t.start_date.equals("")) {
				holder.setText(R.id.text3, "截止使用日期" + t.end_date);

			} else {
				if (t.end_date == null || t.end_date.equals("")) {
					holder.setText(R.id.text3, "起始使用日期" + t.start_date);
				} else {
					holder.setText(R.id.text3, t.start_date + "至" + t.end_date);
				}

			}

		}
		final ImageView img = (ImageView) holder.getView(R.id.img);
		if (bean != null && bean.user_coupons_id.equals(t.user_coupons_id))
			isSeclect = position;
		if (flag == 1 && type == 1) {
			img.setVisibility(View.VISIBLE);
			if (isSeclect == position) {
				img.setImageResource(R.mipmap.coupon_icon_certificate_choose);
			} else {
				img.setImageResource(R.mipmap.coupon_img_certificate_unselected);
			}
		} else {
			img.setVisibility(View.GONE);
		}
		if (type == 1) {
			holder.getConvertView().setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (isSeclect != position) {
						isSeclect = position;
						bean = t;
						MyAppcation.getMyAppcation().notifyDataSetChanged(
								YHQActivity.class, Type.A_YHQ.SECELT_YHQ, bean);
						notifyDataSetChanged();

					} else {
						isSeclect = -1;
						bean = null;
						MyAppcation.getMyAppcation().notifyDataSetChanged(
								YHQActivity.class, Type.A_YHQ.SECELT_YHQ, bean);
						img.setImageResource(R.mipmap.coupon_img_certificate_unselected);
					}

				}
			});
		}

	}

}
